import { 
    updateProfile, 
    uploadAvatar, 
    fetchUserById,
    fetchUserFollowers,
    fetchUserFollowing 
  } from '@/api/user'
  import { fetchCurrentUser } from '@/api/auth'
  
  const state = {
    user: null,
    currentUser: null,
    followers: [],
    following: [],
    loading: false,
    error: null
  }
  
  const getters = {
    isCurrentUser: (state) => (userId) => {
      return state.currentUser?.id === userId
    },
    currentUserFollowing: (state) => {
      return state.currentUser?.following || []
    }
  }
  
  const mutations = {
    SET_USER(state, user) {
      state.user = user
    },
    SET_CURRENT_USER(state, user) {
      state.currentUser = user
    },
    UPDATE_USER(state, userData) {
      state.user = { ...state.user, ...userData }
    },
    UPDATE_CURRENT_USER(state, userData) {
      state.currentUser = { ...state.currentUser, ...userData }
    },
    SET_FOLLOWERS(state, followers) {
      state.followers = followers
    },
    SET_FOLLOWING(state, following) {
      state.following = following
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_ERROR(state, error) {
      state.error = error
    }
  }
  
  const actions = {
    async fetchCurrentUser({ commit }) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const user = await fetchCurrentUser()
        commit('SET_CURRENT_USER', user)
        return user
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async fetchUser({ commit }, userId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const user = await fetchUserById(userId)
        commit('SET_USER', user)
        return user
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async updateProfile({ commit }, profileData) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const updatedUser = await updateProfile(profileData)
        commit('UPDATE_CURRENT_USER', updatedUser)
        return updatedUser
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async uploadAvatar({ commit }, file) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const avatarUrl = await uploadAvatar(file)
        commit('UPDATE_CURRENT_USER', { avatarUrl })
        return avatarUrl
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async fetchUserFollowers({ commit }, userId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const followers = await fetchUserFollowers(userId)
        commit('SET_FOLLOWERS', followers)
        return followers
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async fetchUserFollowing({ commit }, userId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const following = await fetchUserFollowing(userId)
        commit('SET_FOLLOWING', following)
        return following
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    }
  }
  
  export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
  }